﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Oracle.DataAccess.Client;

namespace DBtuning
{
    public partial class Debug : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonClearQuery_Click(object sender, EventArgs e)
        {
            TextBoxQuery.Text = "";
        }

        protected void ButtonExecute_Click(object sender, EventArgs e)
        {
            string d = "\t";
            int fixedLengthWidth = 15;
            try
            {
                fixedLengthWidth = int.Parse(ConfigurationManager.AppSettings["DebugResultFixedWidthLength"]);
            }
            catch { }

            OracleConnection con = null;
            OracleCommand cmd = null;
            OracleTransaction txn = null;
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            try
            {
                con = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConnection"].ConnectionString);
                cmd = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                string query = TextBoxQuery.Text;
                if (query.EndsWith(";"))
                {
                    query = query.Substring(0, query.LastIndexOf(";"));
                }
                cmd.CommandText = query;
                con.Open();
            }
            catch (Exception ex)
            {
              
                sb = new System.Text.StringBuilder();
                sb.Append("Error: " + ex.Message);
                return;
            }
           
                OracleDataReader rdr = null;
                txn = con.BeginTransaction(IsolationLevel.ReadCommitted);
                try
                {
                    cmd.Transaction = txn;
                    rdr = cmd.ExecuteReader(CommandBehavior.Default);
                    int fc = 0;
                    object[] objs = null;
                    int rowCount = 0;
                    while (rdr.Read())
                    {
                        fc = rdr.FieldCount;
                        objs = new object[fc];
                        if (rowCount == 0)
                        {
                            for (int c = 0; c < fc; c++)
                            {
                                if (d == " ")
                                {
                                    if (rdr.GetName(c).Length > fixedLengthWidth)
                                        sb.Append(rdr.GetName(c).Substring(0, fixedLengthWidth));
                                    else
                                        sb.Append(rdr.GetName(c).PadRight(fixedLengthWidth));
                                }
                                else
                                {
                                    sb.Append(rdr.GetName(c));
                                }
                                sb.Append(d);
                            }
                            sb.Append("\r\n");
                        }
                        rowCount++;
                        if (rdr.GetValues(objs) > 0)
                        {
                            foreach (object o in objs)
                            {
                                if (d == " ")
                                {
                                    if (o.ToString().Length > fixedLengthWidth)
                                        sb.Append(o.ToString().Substring(0, fixedLengthWidth));
                                    else
                                        sb.Append(o.ToString().PadRight(fixedLengthWidth));
                                }
                                else
                                {
                                    sb.Append(o.ToString());
                                }
                                sb.Append(d);
                            }
                        }
                        sb.Append("\r\n");
                    }
                    txn.Commit();
                    rdr.Close();

                    if (rowCount == 0) //successful execution of non-query
                    {
                        sb.Append("Query executed successfully");
                    }

                }
                catch (Exception ex)
                {
                    txn.Rollback();
                    sb = new System.Text.StringBuilder();
                    sb.Append("Error: Transaction rolled back\r\n");
                    sb.Append(ex.Message);

                }
                con.Close();
          

            TextBoxResult.Text = sb.ToString();
        }

        protected void ButtonClear_Click(object sender, EventArgs e)
        {
            TextBoxQuery.Text = "";
            TextBoxResult.Text = "";
        }

        protected void ButtonClearResult_Click(object sender, EventArgs e)
        {
            TextBoxResult.Text = "";
        }
    }
}
